﻿using System;
using System.Diagnostics;
using System.Reflection;

namespace AEM.Utils
{
    /// <summary>
    /// bug收集
    /// </summary>
    [Serializable]
    public class BubReport
    {
        /// <summary>
        /// 版本号
        /// </summary>
        protected string BubReportVersion { get; set; }
        /// <summary>
        /// ip
        /// </summary>
        protected string[] ip { get; set; }
        /// <summary>
        /// 机器系统版本
        /// </summary>
        protected string OSVersion { get; set; }
        /// <summary>
        /// 异常描述
        /// </summary>
        public string message { get; set; }
        /// <summary>
        /// 错误级别
        /// </summary>
        public string Level { get; set; }
        /// <summary>
        /// 错误的描述信息
        /// </summary>
        public string tracert { get; set; }
        /// <summary>
        /// 文件安装路径
        /// </summary>
        protected string programeInsatallPath { get; set; }

        /// <summary>
        /// 文件版本
        /// </summary>
        protected string programeVer { get; set; }

        /// <summary>
        /// 机器名
        /// </summary>
        protected string PCname { get; set; }
        /// <summary>
        /// 时间
        /// </summary>
        protected DateTime datetime { get; set; }

        public string softLog { get; set; }

        /// <summary>
        /// 日志是否已保存
        /// </summary>
        protected bool isSaveFile = false;
        protected string logFileBasestr;

        public BubReport()
        {
            this.PCname = System.Environment.MachineName;
            this.OSVersion = System.Environment.OSVersion.VersionString;
            this.programeVer = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            this.BubReportVersion = "1.0(2011-10-16)";
            this.datetime = DateTime.Now;
        }
        protected void saveFile()
        {
            if (isSaveFile) return;
            isSaveFile = true;

            string filestr = Json.serialize(this);
            string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName());
            this.logFileBasestr = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(path));
            System.IO.File.WriteAllText(path, filestr, System.Text.Encoding.UTF8);
        }


        /// <summary>
        /// 发送报告
        /// </summary>
        public void Report()
        {
            try
            {
                this.saveFile();
                System.Diagnostics.Process p = new Process();
                p.StartInfo = new ProcessStartInfo { UseShellExecute = true, WindowStyle = ProcessWindowStyle.Hidden };
                p.EnableRaisingEvents = false;
                p.StartInfo.Arguments = this.logFileBasestr;
                p.StartInfo.FileName = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "BugReport.exe");
                bool isstart = p.Start();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                Log.WriteLog(Log.LogType.Debug, ex.ToString());
            }
        }
    }
}